//-----------------------------------------------------------------------------
// <copyright file="PopupHelper.cs" 
// Anjum Rizwi 
// anjumrizwi@gmail.com
// </copyright>
//-----------------------------------------------------------------------------
namespace HTMLService
{
    #region Includes
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;

    using System.IO;
    using System.Collections.Specialized;
    using System.Reflection;
    #endregion

    /// <summary>
    ///  PopupHelper class will contain UI related methods.
    /// </summary>
    public class PopupHelper
    {

        /// <summary>
        /// Create popup using standard master user control for generic purpose. 
        /// </summary>
        /// <param name="path"></param>
        /// <param name="nvc"></param>
        /// <param name="popUpDivId"></param>
        /// <returns></returns>
        public static string RenderPopUserControl(string path, Dictionary<string, object> nvc, string popUpDivId)
        {
            return RenderPopUserControl(PopupSize.Medium, path, nvc, popUpDivId);
        }

        /// <summary>
        /// Renders the pop user control.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="nvc">The NVC.</param>
        /// <returns></returns>
        public static string RenderPopUserControl(PopupSize popupType, string path, Dictionary<string, object> nvc, string popUpDivId)
        {
            string materControlPath = GetMasterControlPath(popupType);

            Dictionary<string, object> childControlInfo = new Dictionary<string, object>();
            childControlInfo.Add("ChildControlPath", path);
            childControlInfo.Add("PopUpDivId", popUpDivId);
            childControlInfo.Add("ChildControlProperty", nvc);
            childControlInfo.Add("PopupSize", popupType);

            return RenderUserControl(materControlPath, childControlInfo);
        }

        /// <summary>
        /// This is overloaded method for stripping the form tag and other viewstaate related hidden input 
        /// that is not required in our case. We need only html
        /// </summary>
        /// <param name="path">User control virtual path</param>
        /// <param name="nvc">Set Public property before rendering </param>
        /// <param name="contentAreaDivId">Content area div id for stripping html</param>
        /// <returns></returns>
        public static string RenderUserControl(string path, Dictionary<string, object> nvc, string contentAreaDivId)
        {
            string html = RenderUserControl(path, nvc);

            if (!string.IsNullOrWhiteSpace(contentAreaDivId))
            {
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(html);

                html = doc.GetElementbyId(contentAreaDivId).InnerHtml;
            }

            return html;
        }

        /// <summary>
        /// This is overloaded method for stripping the form tag and other viewstaate related hidden input 
        /// that is not required in our case. We need only html
        /// </summary>
        public static string RenderUserControl(string path, Dictionary<string, object> nvc)
        {
            Page pageHolder = new Page();
            UserControl viewControl = (UserControl)pageHolder.LoadControl(path);

            if (nvc != null && nvc.Count > 0)
            {
                Type viewControlType = viewControl.GetType();

                foreach (string key in nvc.Keys)
                {
                    PropertyInfo property = viewControlType.GetProperty(key);

                    if (property == null)
                    {
                        throw new Exception(string.Format("'{0}' property is not defined"));
                    }
                    else
                    {
                        property.SetValue(viewControl, nvc[key], null);
                    }
                }
            }

            pageHolder.Controls.Add(viewControl);
            StringWriter output = new StringWriter();
            HttpContext.Current.Server.Execute(pageHolder, output, true);

            return output.ToString();
        }

        public static string RenderDialogUserControl(string popupDivId, PopUpControlType controlType, string PopUpInfoParams, PopupSize popupType)
        {
            string ouputHtml = default(string);
            string folderPath = @"~/Controls/{0}";
            try
            {
                string popupHeaderText = default(string);
                string popUpContent = default(string);
                string childPopup = default(string);
                switch (controlType)
                {
                    case PopUpControlType.Orders:
                        {
                            popupHeaderText = "Order";
                            popUpContent = "All Orders";
                            childPopup = string.Format(folderPath, "Order.ascx");
                        } break;
                    case PopUpControlType.OrderDetails:
                        {
                            popupHeaderText = "Order Info";
                            popUpContent = "Order Info Content";
                            childPopup = string.Format(folderPath, "OrderDetail.ascx");
                        } break;
                    case PopUpControlType.Common:
                        {
                            popupHeaderText = "Generic Popup";
                            popUpContent = "Generic configurable popup content";
                            childPopup = @"~/Dialog/AjaxPopUp/AjaxPopChild.ascx";
                        } break;
                }

                Dictionary<string, object> nvc = new Dictionary<string, object>();
                nvc.Add("PopUpHeader", popupHeaderText);
                nvc.Add("PopUpContent", popUpContent);
                nvc.Add("Params", PopUpInfoParams);
                ouputHtml = HTMLService.PopupHelper.RenderPopUserControl(popupType, childPopup, nvc, popupDivId);
            }
            catch (Exception ex)
            {
                ouputHtml = ex.Message;
            }
            return ouputHtml;
        }

        /// <summary>
        /// Get different type of master control
        /// </summary>
        /// <param name="popupType"></param>
        /// <returns></returns>
        private static string GetMasterControlPath(PopupSize popupType)
        {
            string materControlPath;
            switch (popupType)
            {
                case PopupSize.Small:
                    materControlPath = @"~/Dialog/SmallPopMaster.ascx";
                    break;
                case PopupSize.Medium:
                    materControlPath = @"~/Dialog/MediumPopMaster.ascx";
                    break;
                case PopupSize.Big:
                    materControlPath = @"~/Dialog/BigPopMaster.ascx";
                    break;
                case PopupSize.Default:
                    materControlPath = @"~/Dialog/DialogMaster.ascx";
                    break;
                default:
                    throw new Exception("Master Dialog control path is missing.");
            }
            return materControlPath;
        }
    }
}